/* 
 *
 * Persistence4J - Simple library for data persistence using java
 * Copyright (c) 2010, Avdhesh yadav.
 * http://www.avdheshyadav.com
 * Contact: avdhesh.yadav@gmail.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package com.avdy.p4j.jdbc.dbms;

import org.apache.log4j.Logger;

import com.avdy.p4j.common.DbmsType;
import com.avdy.p4j.jdbc.PersistenceConfig;
import com.avdy.p4j.jdbc.common.CRUD;
import com.avdy.p4j.jdbc.common.GenericCRUD;
import com.avdy.p4j.jdbc.dao.AbsPersistenceManager;
import com.avdy.p4j.jdbc.dao.PgSqlPersistenceManager;
import com.avdy.p4j.jdbc.dbms.connfactory.ConnectionFactory;
import com.avdy.p4j.jdbc.dbms.connfactory.DBConnector;
import com.avdy.p4j.jdbc.dbms.metadata.IMetaDataLoader;
import com.avdy.p4j.jdbc.dbms.metadata.MetaDataLoader;

/**
 * @author Avdhesh Yadav - Avdhesh.Yadav@Gmail.com    
 */
class PgSqlDbms extends Dbms
{
	static Logger logger = Logger.getLogger(PgSqlDbms.class.getName());
	
	private static Dbms _Instance;
	
	/**
	 * 
	 * @param props PersistenceConfig
	 * @return Dbms
	 */
	public static Dbms getInstance(PersistenceConfig props)
	{
		if(_Instance == null)
		{
			_Instance = new PgSqlDbms(props);
		}
		return _Instance;
	}
	
	
	/**
	 * 
	 * @param config PersistenceConfig
	 */
	private PgSqlDbms(PersistenceConfig config)
	{
		super(config);
		m_ConnFactory = ConnectionFactory.getConnectionFactory(DbmsType.PGSQL);
		mDsAttr = constructDataSourceAttr(DbmsType.PGSQL);
	}
	
	
	/**
	 * 
	 * @param database String
	 * 
	 * @throws Exception
	 */
	public void createDataSource(String database, boolean isTransactional) throws Exception
	{
		logger.info("createDataSource in pgsql:" + database);
		
		boolean isNew = isNewDataSource(database);
		if(isNew)
		{
			//create new data source and bound it to the map
			DBConnector connector = getDBConnector(database ,isTransactional);
			IMetaDataLoader loader = new MetaDataLoader(connector);
			CRUD crud = new GenericCRUD();
			AbsPersistenceManager pcManager = new PgSqlPersistenceManager(loader,crud);
			m_PcManagerMap.put(database, pcManager);
		}
	}
}